package com.cloudbroker.bcs.platform.dav.middle.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;

public class URLcoderUtil {

    private static Logger logger = Logger.getLogger(URLcoderUtil.class);

    private URLcoderUtil() {
    }

    public static String encoder(String url) {
        String encodeUrl = url;
        try {
            encodeUrl = URLEncoder.encode(encodeUrl, "utf8");
        } catch (UnsupportedEncodingException e) {
            logger.error("URLEncoder.encode encode=utf8,url=" + encodeUrl, e);
        }
        return encodeUrl;

    }

    public static Map<String, String> encoder(Map<String, String> map) {

        Map<String, String> data = new HashMap<String, String>();
        try {

            for (Entry<String, String> entry : map.entrySet()) {
                String key = entry.getKey();
                String url = URLEncoder.encode(entry.getValue(), "utf8");
                data.put(key, url);
            }
        } catch (UnsupportedEncodingException e) {
            logger.error("URLEncoder.encode encode=utf8,map=" + JSON.toJSONString(map), e);
        }
        return data;

    }

    public static Map<String, String> decoder(Map<String, String> map) {

        Map<String, String> data = new HashMap<String, String>();
        try {

            for (Entry<String, String> entry : map.entrySet()) {
                String key = entry.getKey();
                String url = URLDecoder.decode(entry.getValue(), "utf8");
                data.put(key, url);
            }
        } catch (UnsupportedEncodingException e) {
            logger.error("URLEncoder.decode encode=utf8,map=" + JSON.toJSONString(map), e);
        }
        return data;

    }

    public static String decoder(String url) {
        String decoderUrl = url;
        try {
            decoderUrl = URLDecoder.decode(decoderUrl, "utf8");
        } catch (UnsupportedEncodingException e) {
            logger.error("URLEncoder.decoder encode=utf8,url=" + decoderUrl, e);
        }
        return decoderUrl;

    }

}
